﻿	<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core'%>
	<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
	
	<style type="text/css">
		#dlg-goods input, #dlg-goods select
		{
			width: 150px;
		}
		#dlg-goods #fm-goods div 
		{
			float: left;
			width: 300px;
		}
		#dlg-goods #fm-goods div label
		{
			width: 80px;
		}
	</style>
	<script type="text/javascript">
		var goodsGridDetailFormatter = function(index,row){
			if(row.goods) 
			{
				return '<div class="ddv-goods" style="padding:5px 0"></div>';
			}
		}
		var goodsGridExpandRow = function(index,row){
			var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv-goods');
			ddv.panel({
				height:65,
				border:false,
				cache:false,
				content:"<style type='text/css'>.dv-table-goods td{border:0;}.dv-label-goods{font-weight:normal;color:#15428B;width:10%;}.dv-nonlabel-goods{width:40%;}</style>" + 
				"<table class='dv-table-goods' border='0' style='width:100%;'>" + 
				"<tr><td class='dv-label-goods'>条形码:</td><td class='dv-nonlabel-goods'>" + row.barCode + "</td></tr>" + 
				"<tr><td class='dv-label-goods'>制造者:</td><td class='dv-nonlabel-goods'>" + row.maker + "</td><td class='dv-label-goods'>规格:</td><td class='dv-nonlabel-goods'>" + row.specification + "</td></tr>" + 
				"<tr><td class='dv-label-goods'>参数:</td><td class='dv-nonlabel-goods'>" + row.technicalParameters + "</td><td class='dv-label-goods'></td><td class='dv-nonlabel-goods'></td></tr>" + 
				"</table>",
				onLoad:function(){
					$('#dg-goods').datagrid('fixDetailRowHeight',index);
				}
			});
			$('#dg-goods').datagrid('fixDetailRowHeight',index);
		}
		$.extend($.fn.validatebox.defaults.rules, {
			checkPrice: {
				validator: function(value, param){
					var importPrice;
					if(param[0].startsWith('#')) 
					{
						importPrice = $(param[0]).numberbox('getValue');
					}
					if(param[0] != '') 
					{
						return parseNumber(value) >= importPrice;
					}
				},
				message: '零售价和批发价不能低于进货价'
			}
		});
		
		function popupSelectGoodsTypeDialog() 
		{
			var goodsTypeId = $('#dlg-goods #fm-goods #goodsTypeId').val();
			loadTreeData ('#dlg-goodsType-selection #tt_goodsType', '<c:url value='/goodsType/getAllType.html' />', goodsTypeId);
			$('#dlg-goodsType-selection').dialog('open').dialog('setTitle', '选择类型');
		}
		
		function selectGoodsType() 
		{
			//var checkedNodes = $('#dlg-goodsType-selection #tt_goodsType').tree('getChecked');
			var selectedNode = $('#dlg-goodsType-selection #tt_goodsType').tree('getSelected');
			//if (checkedNodes.length == 1) 
			if (selectedNode) 
			{
				$('#dlg-goods #fm-goods #goodsTypeId').val(selectedNode.id);
				//$('#dlg-goods #fm-goods #goodsTypeName').val(selectedNode.text);
				$('#dlg-goodsType-selection').dialog('close');
			}
			else 
			{
				showWarningMsg ('请选择一个类型.');
			}
		}
		var newGoodsCallback = function () 
		{
			$('#fm-goods input.easyui-combobox').combobox('reload');
			$('#dlg-goods #fm-goods #available').combobox('setValue', true);
			$('#dlg-goods #fm-goods #available').combobox('setText', '可用');
			
			var goods = $('#dg-goods').datagrid('getSelected');
			if(goods && !goods.goods) 
			{
				//$('#dlg-goods #fm-goods #goodsTypeId').combotree('reload', '<c:url value='/goodsType/getAllType.html' />');
				//$('#dlg-goods #fm-goods #goodsTypeId').combotree('setValue', goods.id);
				fillSelectedGoodsType ('<c:url value='/goodsType/getAllType.html' />', '#dlg-goods #fm-goods #goodsTypeId', goods.id);
			}
			$('#dlg-goods #fm-goods #initialStock').numberbox('enable');
			$('#dlg-goods #fm-goods #minStock').numberbox('enable');
			$('#dlg-goods #fm-goods #maxStock').numberbox('enable');
			$('#dlg-goods #fm-goods #initialStock').numberbox('setValue', 0);
			$('#dlg-goods #fm-goods #minStock').numberbox('setValue', 0);
			$('#dlg-goods #fm-goods #maxStock').numberbox('setValue', 0);
		}
		var editGoodsPredo = function () 
		{
			var goods = $('#dg-goods').datagrid('getSelected');
			if(goods && goods.goods) 
			{
				editModel('#dg-goods', '#dlg-goods', '<spring:message code="editGoods" />', '#fm-goods', '<c:url value='/goods/updateGoods.html' />', editGoodsCallback);
			}
		}
		var deleteGoodsPredo = function () 
		{
			var goods = $('#dg-goods').datagrid('getSelected');
			if(goods && goods.goods) 
			{
				destroyModel('#dg-goods', '<spring:message code="goods" />', '<c:url value='/goods/delSingleGoods.html' />', saveGoodsCallback);
			}
		}
		var editGoodsCallback = function () 
		{
			$('#fm-goods input.easyui-combobox').combobox('reload');
			var goods = $('#dg-goods').datagrid('getSelected');
			if(goods.typeBean) 
			{
				fillSelectedGoodsType ('<c:url value='/goodsType/getAllType.html' />', '#dlg-goods #fm-goods #goodsTypeId', goods.typeBean.id);
			}
			if(goods.available) 
			{
				$('#dlg-goods #fm-goods #available').combobox('setValue', goods.available);
				$('#dlg-goods #fm-goods #available').combobox('setText', '可用');
			}
			else 
			{
				$('#dlg-goods #fm-goods #available').combobox('setValue', goods.available);
				$('#dlg-goods #fm-goods #available').combobox('setText', '不可用');
			}
			$('#dlg-goods #fm-goods #initialStock').numberbox('disable');
			$('#dlg-goods #fm-goods #minStock').numberbox('disable');
			$('#dlg-goods #fm-goods #maxStock').numberbox('disable');
			if(goods.storageBean) 
			{
				//console.log(goods.storageBean);
				$('#dlg-goods #fm-goods #initialStock').numberbox('setValue', goods.storageBean.initialStock);
				$('#dlg-goods #fm-goods #minStock').numberbox('setValue', goods.storageBean.minStock);
				$('#dlg-goods #fm-goods #maxStock').numberbox('setValue', goods.storageBean.maxStock);
			}
			if(goods.unitBean) 
			{
				$('#dlg-goods #fm-goods #unit').combobox('setValue', goods.unitBean.id);
			}
		}
		var fillSelectedGoodsType = function(requestUrl, combotreeId, selectedId) 
		{
			$.post(requestUrl, 
				function(result) 
				{
					//console.log(result);
					$(combotreeId).combotree('loadData', result);
					$(combotreeId).combotree('setValue', selectedId);
				}, 
				'json');
		}
		
		var onSelectGoodsType = function (node) 
		{
			$('#dlg-buttons-goodsType-selection #save-goodsType').click ();
		}
		var saveGoodsCallback = function () 
		{
			$('#dg-goods').treegrid('reload'); 
		}
		var cellFormatter_goodsImportPrice = function(value,row,index) 
		{
			if(row.goods) 
			{
				return value;
			}
		}
		var cellFormatter_goodsUnit = function(value,row,index) 
		{
			if(row.goods && row.unitBean) 
			{
				return row.unitBean.name;
			}
		}
		function cellStyler_goodsName(value,row,index)
		{
			if(row.goods) 
			{
				return "color:blue";
			}
		}
		
		var onClickGoodsRow = function (row) 
		{
			if(row.goods) 
			{
				$('#toolbar-goods #btn-editGoods').linkbutton('enable');
				$('#toolbar-goods #btn-deleteGoods').linkbutton('enable');
			}
			else 
			{
				$('#toolbar-goods #btn-editGoods').linkbutton('disable');
				$('#toolbar-goods #btn-deleteGoods').linkbutton('disable');
			}
		}
		var onLoadGoodsSuccess = function (data) 
		{
			$('#toolbar-goods #btn-editGoods').linkbutton('disable');
			$('#toolbar-goods #btn-deleteGoods').linkbutton('disable');
		}
		var onGoodsRowContextMenu = function(e, row) 
		{
			e.preventDefault();
			$(this).treegrid('select',row.id);
			onClickGoodsRow(row);
			$('#mm_goods').menu('show',{
				left: e.pageX,
				top: e.pageY
			});
		}
		
		var newGoodsContextMenu = function() 
		{
			var goods = $('#dg-goods').datagrid('getSelected');
			newModel('#dlg-goods', '<spring:message code="newGoods" />', '#fm-goods', '<c:url value='/goods/newGoods.html' />', newGoodsCallback);
		}
		var onShowGoodsMenu = function() 
		{
			var goods = $('#dg-goods').datagrid('getSelected');
			if(goods.goods) 
			{
				var updateItem = $('#mm_goods').menu('findItem', '<spring:message code="editGoods" />');
				$('#mm_goods').menu('enableItem', updateItem.target);
				var removeItem = $('#mm_goods').menu('findItem', '<spring:message code="removeGoods" />');
				$('#mm_goods').menu('enableItem', removeItem.target);
			}
			else 
			{
				var updateItem = $('#mm_goods').menu('findItem', '<spring:message code="editGoods" />');
				$('#mm_goods').menu('disableItem', updateItem.target);
				var removeItem = $('#mm_goods').menu('findItem', '<spring:message code="removeGoods" />');
				$('#mm_goods').menu('disableItem', removeItem.target);
			}
		}
		var showAllGoodsType = function () 
		{
			$(this).combotree('reload', '<c:url value='/goodsType/getAllType.html' />');
		}
	</script>

	<div style="width:100%; height:450px;">
		<table id="dg-goods" title="<spring:message code="goodsManagement" />" class="easyui-treegrid" url="<c:url value='/goods/asyncGetGoods2.html' />" 
			toolbar="#toolbar-goods" rownumbers="true" singleSelect="true" checkOnSelect="true" selectOnCheck="false" 
			fitColumns="true" showFooter="true" 
			data-options="idField:'id',treeField:'name', fit:true, onClickRow:onClickGoodsRow, onLoadSuccess:onLoadGoodsSuccess, onContextMenu:onGoodsRowContextMenu">
			<thead>
				<tr>
					<!-- <th data-options="field:'ck',checkbox:true"></th> -->
					<th data-options="field:'id',hidden:true"></th>
					<th field="name" width="150" data-options="styler:cellStyler_goodsName"><spring:message code="name" /></th>
					<th field="importPrice" width="50" sortable="false" data-options="formatter:cellFormatter_goodsImportPrice"><spring:message code="importPrice" /></th>
					<th field="retailPrice" width="50" sortable="false" data-options="formatter:cellFormatter_goodsImportPrice"><spring:message code="retailPrice" /></th>
					<th field="tradePrice" width="50" sortable="false" data-options="formatter:cellFormatter_goodsImportPrice"><spring:message code="tradePrice" /></th>
					<th field="unit" width="50" data-options="formatter:cellFormatter_goodsUnit"><spring:message code="unit" /></th>
					<th field="available" width="50" sortable="false" data-options="formatter:cellFormatter_goodsAvailable, align:'center'"><spring:message code="available" /></th>
					
					<!-- hidden column -->
					<th field="goods" width="50" hidden="true"></th>
					<th field="maker" width="50" hidden="true"><spring:message code="maker" /></th>
					<th field="barCode" width="50" hidden="true"><spring:message code="barCode" /></th>
					<th field="specification" width="50" hidden="true"><spring:message code="specification" /></th>
					<th field="technicalParameters" width="50" hidden="true"><spring:message code="technicalParameters" /></th>
				</tr>
			</thead>
		</table>
	</div>
	<div id="toolbar-goods">
		<a href="javascript:void(0)" class="easyui-linkbutton" 
			iconCls="icon-add" plain="true" onclick="newModel('#dlg-goods', '<spring:message code="newGoods" />', '#fm-goods', '<c:url value='/goods/newGoods.html' />', newGoodsCallback)" title="<spring:message code="newGoods" />"><spring:message code="newGoods" /></a> 
		<a href="javascript:void(0)" class="easyui-linkbutton" id="btn-editGoods" disabled="true" 
			iconCls="icon-edit" plain="true" onclick="editGoodsPredo()" title="<spring:message code="selectOneRow" /><spring:message code="editGoods" />"><spring:message code="editGoods" /></a> 
		<a href="javascript:void(0)" class="easyui-linkbutton" id="btn-deleteGoods" disabled="true" 
			iconCls="icon-remove" plain="true" onclick="deleteGoodsPredo()" title="<spring:message code="selectOneRow" /><spring:message code="removeGoods" />"><spring:message code="removeGoods" /></a>
		<%-- <span style="float: right; margin-right: 5px">
			<input class="easyui-searchbox" data-options="prompt:'<spring:message code="pleaseInputValue" />',searcher:doSearchGoods" style="width:300px" />
		</span> --%>
	</div>
	
	<!-- 右键菜单 -->
	<div id="mm_goods" class="easyui-menu" style="width:120px;" data-options="onShow:onShowGoodsMenu">
        <div data-options="iconCls:'icon-add'" onclick="newGoodsContextMenu()">
			<spring:message code="newGoods" />
		</div>
        <div data-options="iconCls:'icon-edit'" onclick="editGoodsPredo()">
			<spring:message code="editGoods" />
		</div>
        <div data-options="iconCls:'icon-remove'" onclick="deleteGoodsPredo()">
			<spring:message code="removeGoods" />
		</div>
    </div>
	<!-- 右键菜单 -->

	<div id="dlg-goods" class="easyui-dialog"
		style="width: 750px; height: 320px; padding: 5px;" closed="true"
		buttons="#dlg-buttons-goods" data-options="modal:true">
		<div style="padding: 0px 20px">
			<form id="fm-goods" class="fm" method="post" novalidate>
				<div class="fitem divHidden">
					<input id="goodsId" name="id" value="0">
				</div>
				<div class="fitem">
					<label><spring:message code="name" />:</label> 
					<input name="name" class="easyui-validatebox" required="true" 
					validType="myRemote['<c:url value='/goods/checkExist.html' />', 'name', '#goodsId']" >
				</div>
				<div class="fitem">
					<label><spring:message code="importPrice" />:</label> 
					<input id="importPrice" name="importPrice" class="easyui-numberbox" data-options="precision:2, required:true" style="width:156px">
				</div>
				<div class="fitem">
					<label><spring:message code="retailPrice" />:</label> 
					<input name="retailPrice" class="easyui-numberbox" data-options="precision:2, required:true" validType="checkPrice['#dlg-goods #fm-goods #importPrice']" style="width:156px">
				</div>
				<div class="fitem">
					<label><spring:message code="tradePrice" />:</label> 
					<input name="tradePrice" class="easyui-numberbox" data-options="precision:2, required:true" validType="checkPrice['#dlg-goods #fm-goods #importPrice']" style="width:156px">
				</div>
				<div class="fitem">
					<label><spring:message code="unit" />:</label> 
					<input id="unit" name="unitBean.id" class="easyui-combobox" panelHeight="auto" style="width:150px" editable="false"
						data-options="valueField:'id',textField:'name',method:'post',url:'<c:url value='/goodsUnit/getAllUnit.html' />'">
				</div>
				<div class="fitem">
					<label>初始库存:</label> 
					<input id="initialStock" name="storageBean.initialStock" class="easyui-numberbox" data-options="" style="width:156px">
				</div>
				<div class="fitem">
					<label>最小库存:</label> 
					<input id="minStock" name="storageBean.minStock" class="easyui-numberbox" data-options="" style="width:156px">
				</div>
				<div class="fitem">
					<label>最大库存:</label> 
					<input id="maxStock" name="storageBean.maxStock" class="easyui-numberbox" data-options="" style="width:156px">
				</div>
				<div class="fitem">
					<label><spring:message code="available" />:</label> 
					<select id="available" name="available" class="easyui-combobox" style="width:150px" editable="false" required="true" panelHeight="auto">
						<option value="true" selected="selected"><spring:message code="enable" /></option>
						<option value="false"><spring:message code="unavailable" /></option>
					</select>
				</div>
				<div class="fitem">
					<label><spring:message code="barCode" />:</label> 
					<input name="barCode">
				</div>
				<div class="fitem">
					<label><spring:message code="maker" />:</label> 
					<input name="maker" class="easyui-validatebox">
				</div>
				<div class="fitem">
					<label><spring:message code="specification" />:</label> 
					<input name="specification" class="easyui-validatebox">
				</div>
				<div class="fitem">
					<label><spring:message code="technicalParameters" />:</label> 
					<input name="technicalParameters" class="easyui-validatebox">
				</div>
				<!--<div class="fitem divHidden">
					<input id="goodsTypeId" name="goodsTypeId" readonly="readonly" />
					<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" 
						title="<spring:message code="goods.deleteGoodsType" />"
						onclick="cleanInput(['#dlg-goods #fm-goods #goodsTypeId','#dlg-goods #fm-goods #goodsTypeName'])"></a> 
				</div>-->
				<div class="fitem">
					<label><spring:message code="goods.type" />:</label> 
					<input id="goodsTypeId" name="typeBean.id" class="easyui-combotree" panelHeight="auto" title="<spring:message code="goods.selectAnType" />" 
						style="width:150px" data-options="url:'', onShowPanel:showAllGoodsType" />
				</div>
			</form>
		</div>
	</div>
	<div id="dlg-buttons-goods">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-ok" onclick="saveModelForGrid('#dg-goods', '#dlg-goods', '#fm-goods', saveGoodsCallback)"><spring:message code="save" /></a> 
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-cancel" onclick="javascript:$('#dlg-goods').dialog('close')"><spring:message code="cancel" /></a>
	</div>
